<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>使用工厂方法创建对象</title>
</head>
<body>
<script>
    /*
     *
   */
    var obj={
        name:"唐僧",
        gender:"男",
        age:25,
        sayName:function(){
            console.log(this.name)
        }
    };
    //用工厂方法创建对象
function createPerson(name,age){
    var obj=new Object();//用构造函数定义对象
        //向变量中添加属性
        obj.name=name;
        obj.age=age;
        obj.sayName=function () {
            document.write(this.name);
        }
        //将新的对象返回
    return obj;
}
var obj2=createPerson("孙悟空",15);
var obj3=createPerson("猪八戒",15);
obj3.sayName();

</script>
</body>
</html>